import pandas as pd
from grid_feature import *
import os

max_longitude = 118.2007
min_longitude = 118.0635
max_latitude = 24.5664
min_latitude = 24.4214
num = 20


# 输出绘制热力图的文件
def heat_map():
    file_path = './data/'
    num = 20
    interval = 60
    business = pd.read_csv(file_path + str(num)+'_'+str(interval)+'business_final.csv')
    grid_num = len(business['grid_id'].unique())
    poi = pd.read_csv(file_path + str(num) + 'counts_POI.csv')
    weather = pd.read_csv(file_path + 'weather_data.csv')
    data = pd.merge(business, poi, how='left').fillna(0)
    data = pd.merge(data, weather, how='left', on=['month_day', 'hour']).fillna(0)
    business_step1 = pd.read_csv('./data/' + str(num) + '_' + str(interval) + 'business_step1.csv')
    scalar = max(business_step1['counts']) - min(business_step1['counts'])

    grid_info = divide_grid(num, max_longitude, min_longitude, max_latitude, min_latitude)
    grid_info['lng'] = (grid_info['grid_min_longitude'] + grid_info['grid_max_longitude'])/2
    grid_info['lat'] = (grid_info['grid_max_latitude'] + grid_info['grid_min_latitude'])/2
    grid_info = grid_info.drop(['grid_min_longitude', 'grid_max_longitude', 'grid_min_latitude', 'grid_max_latitude'], axis=1)

    data = data[['grid_id', 'counts']]
    counts_sum = data.groupby('grid_id').sum().reset_index()

    result = pd.merge(counts_sum, grid_info, how='left')
    result['counts'] = result['counts']*scalar

    file_path = './data/heatmap.txt'
    with open(file_path, 'w') as output_file:
        for index, row in result.iterrows():
            output_file.write("{\"lng\": "+str(round(row['lng'], 6))
                              + ", \"lat\": "+str(round(row['lat'], 6))
                              + ", \"count\": "+str(int(row['counts']))+"},\n")
    output_file.close()


# 输出绘制格子图的文件
def grid_map():
    longitude_unit = (max_longitude - min_longitude) / num
    latitude_unit = (max_latitude - min_latitude) / num
    longitude_edge = [min_longitude + longitude_unit * i for i in range(0, num + 1)]
    latitude_edge = [min_latitude + latitude_unit * i for i in range(0, num + 1)]
    file_path = './data/gridmap.txt'
    with open(file_path, 'w') as output_file:
        for lng in longitude_edge:
            output_file.write(str(round(lng, 6))+","+str(round(latitude_edge[0], 6))+',')
            output_file.write(str(round(lng, 6)) + "," + str(round(latitude_edge[-1], 6)) + ',')
            output_file.write('\n')
        for lat in latitude_edge:
            output_file.write(str(round(longitude_edge[0], 6))+","+str(round(lat, 6))+',')
            output_file.write(str(round(longitude_edge[-1], 6))+","+str(round(lat, 6))+',')
            output_file.write('\n')
    output_file.close()


# 输出绘制热力格子图的文件
def heat_grid_map():
    file_path = './data/'
    num = 40
    interval = 60
    month_length = 31
    time_chunk_size = 60

    if not os.path.exists(file_path + str(num) + '_' + str(interval) + 'business_step1.csv'):
        from business_feature import business_step1, business_step2
        business_step1(num, max_longitude, min_longitude, max_latitude, min_latitude, interval)
        business_step2(num, interval, month_length, time_chunk_size)
    if not os.path.exists(file_path + str(num) + 'counts_POI.csv'):
        from process_POI import poi_process
        poi_process(num, max_longitude, min_longitude, max_latitude, min_latitude)
    if not os.path.exists(file_path + 'weather_data.csv'):
        from process_weather import weather_process
        weather_process()

    business = pd.read_csv(file_path + str(num) + '_' + str(interval) + 'business_final.csv')
    poi = pd.read_csv(file_path + str(num) + 'counts_POI.csv')
    weather = pd.read_csv(file_path + 'weather_data.csv')
    data = pd.merge(business, poi, how='left').fillna(0)
    data = pd.merge(data, weather, how='left', on=['month_day', 'hour']).fillna(0)
    business_step1 = pd.read_csv('./data/' + str(num) + '_' + str(interval) + 'business_step1.csv')
    scalar = max(business_step1['counts']) - min(business_step1['counts'])
    grid_num = len(business_step1['grid_id'].unique())
    grid_info = divide_grid(num, max_longitude, min_longitude, max_latitude, min_latitude)

    data = data[['grid_id', 'counts']]
    counts_sum = data.groupby('grid_id').sum().reset_index()

    result = pd.merge(counts_sum, grid_info, how='left')
    result['counts'] = result['counts']*scalar
    print(max(result['counts']))

    file_path = './data/heatgridmap.txt'
    with open(file_path, 'w') as output_file:
        for index, row in result.iterrows():
            output_file.write(str(round(row['grid_min_longitude'], 6))+','+str(round(row['grid_max_latitude'], 6))+','
                              + str(round(row['grid_max_longitude']-row['grid_min_longitude'], 6))+','
                              + str(round(row['grid_max_latitude']-row['grid_min_latitude'], 6))+','
                              + str(int(row['counts']))+"\n")
    output_file.close()


heat_grid_map()
